package com.neuedu.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.neuedu.common.Constants;
import com.neuedu.pojo.Result;
import com.neuedu.pojo.TAccount;
import com.neuedu.pojo.TNurse;
import com.neuedu.pojo.dto.nurse.AddNurseDTO;
import com.neuedu.pojo.dto.nurse.DeleteNurseDTO;
import com.neuedu.pojo.dto.nurse.NurseInfoDTO;
import com.neuedu.service.ITAccountService;
import com.neuedu.service.ITNurseService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import java.io.UnsupportedEncodingException;
import java.time.LocalDateTime;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhangsan
 * @since 2024-06-16
 */
@RestController
@RequestMapping("/nurse")
public class TNurseController {
    @Autowired
    ITNurseService nurseService;
    @Autowired
    ITAccountService accountService;
    @GetMapping("/page")
    public Result<List<NurseInfoDTO>> getPage(@RequestParam(name="currentPage",defaultValue = "1")Integer currentPage,
                                              @RequestParam(name="pageSize",defaultValue = "10")Integer pageSize,
                                              // 需要条件查询的字段数据
                                              @RequestParam(name="nurseName",defaultValue = "",required = false)String nurseName,
                                              @RequestParam(name="bindingState",defaultValue = "",required = false)Integer bindingState,
                                              @RequestParam(name="nurseWorkstate",defaultValue = "",required = false)Integer nurseWorkstate){
        return nurseService.getPage(currentPage,pageSize,nurseName,bindingState,nurseWorkstate);
    }

    @PostMapping("/add")
    @Transactional
    public Result add(@RequestBody AddNurseDTO addNurseDTO) throws UnsupportedEncodingException {
        System.out.println(addNurseDTO);
        TAccount one = accountService.getOne(new LambdaQueryWrapper<TAccount>()
                .eq(TAccount::getAccUsername, addNurseDTO.getNurseNumber()));
        if (one != null) {
            return Result.fail(Constants.NUMBER_EXIST);
        }
        //插入nurse表
        TNurse nurse = new TNurse();
        BeanUtil.copyProperties(addNurseDTO, nurse);
        nurse.setBindingState(false);
        nurse.setCreateTime(LocalDateTime.now());
        nurse.setUpdataTime(LocalDateTime.now());
        nurse.setNurseWorkstate(false);
        int nurseRows = nurseService.add(nurse);
        //插入account表
        TAccount account = new TAccount();
        BeanUtil.copyProperties(addNurseDTO, account);
        account.setAccPassword(addNurseDTO.getAccPassword());
        account.setAccUsername(addNurseDTO.getNurseNumber());
        account.setAccType(1);
        account.setCreateTime(LocalDateTime.now());
        account.setUpdateTime(LocalDateTime.now());
        int accountRows = accountService.add(account);
        //返回结果
        if (nurseRows <= 0 || accountRows <= 0) {
            return Result.fail(Constants.NURSE_ADD_FAIL);
        }
        return Result.success(Constants.NURSE_ADD_SUCCESS);
    }
    @GetMapping("/quary/{nurseNumber}")
    public Result findBynurseNumber(@PathVariable("nurseNumber")int nurseNumber){
        return nurseService.findBynurseNumber(nurseNumber);
    }
    @PostMapping("/edit")
    @Transactional
    public Result edit(@RequestBody NurseInfoDTO nurseInfoDTO){
        TAccount one = accountService.getById(nurseInfoDTO.getAccId());
        if (!one.getAccUsername().equals(nurseInfoDTO.getNurseNumber().toString())) {
            TAccount t = accountService.getOne(new LambdaQueryWrapper<TAccount>()
                    .eq(TAccount::getAccUsername, nurseInfoDTO.getNurseNumber()));
            if (t != null) {
                return Result.fail(Constants.NUMBER_EXIST);
            }
        }
        TNurse tNurse = new TNurse();
        BeanUtils.copyProperties(nurseInfoDTO,tNurse);
        tNurse.setUpdataTime(LocalDateTime.now());
        nurseService.updateById(tNurse);
        TAccount tAccount = new TAccount();
        tAccount.setAccId(nurseInfoDTO.getAccId());
        tAccount.setAccUsername(nurseInfoDTO.getNurseNumber().toString());
        tAccount.setAccPassword(nurseInfoDTO.getPassword());
        accountService.updateById(tAccount);
        return Result.success();
    }

    @PostMapping("/delete")
    @Transactional
    public Result edit(@RequestBody DeleteNurseDTO dto){
        accountService.getBaseMapper().deleteById(dto.getAccId());
        nurseService.getBaseMapper().deleteById(dto.getNurseId());
        return Result.success();
    }
    @GetMapping("queryall")
    public Result findAll(){
        return nurseService.findAll();
    }
    @PostMapping("/modbindstate")
    public Result modBindingStateById(@RequestParam(name = "nurseId")Integer nurseId){
        return nurseService.modBindingStateById(nurseId);
    }
}
